#include "mySort.h"
#include <iostream>
#include <string>

MySort::MySort() {
	this->num = 0;
	this->origin = NULL;
}

MySort::MySort(int num, int* origin) {
	this->num = num;
	this->origin = new int[num];
	for (int i = 0; i < num; i++) {
		this->origin[i] = origin[i];
	}
}

MySort::~MySort() {

}

void MySort::changeSwap(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;
}

string MySort::printNum() {
	string result = "";
	for (int i = 0; i < num; i++) {
		result += to_string(origin[i]) + " ";
	}
	return result;
}

string MySort::paritySort() {
	bool oSorted = false;
	bool evenSorted = false;
	while (!oSorted || !evenSorted) {
		int base = 1;
		oSorted = true;
		evenSorted = true;

		for (int i = base + 1; i < num; i += 2) {
			if (origin[i - 1] > origin[i]) {
				changeSwap(origin[i - 1], origin[i]);
				oSorted = false;
				cout << printNum() << endl;
			}
		}

		for (int i = base; i < num; i++) {
			if (origin[i - 1] > origin[i]) {
				changeSwap(origin[i - 1], origin[i]);
				evenSorted = false;
				cout << printNum() << endl;
			}
		}
	}
	return "";
}

string MySort::bubbleSort() {
	bool swapped;
	int start = 0;
	int end = num - 1;
	while (start < end) {
		swapped = false;

		//pass from left to right
		for (int i = start; i < end; i++) {
			if (origin[i] > origin[i + 1]) {
				changeSwap(origin[i], origin[i + 1]);
				cout << printNum() << endl;
				swapped = true;
			}
		}
		if (!swapped)
			break;

		for (int i = end - 1; i > start; i--) {
			if (origin[i] < origin[i - 1]) {
				changeSwap(origin[i], origin[i - 1]);
				cout << printNum() << endl;
				swapped = true;
			}
		}
		if (!swapped)
			break;

		start++;
		end--;
	}
	return "";
}
